package com.yoomiito.app.pay

import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.showToast
import com.yoomiito.app.utils.AllUtils
import com.yoomiito.app.utils.UserInfoConfig
import com.yoomiito.app.widget.dialog.PayDialog

class GeneralPayUtils(context: BaseActivity<*>, payResultListener: (PayResult) -> Unit) : BasePayUtils(context, payResultListener) {

    private val payDialog = PayDialog(context).also { it ->
        it.setOnResultListener { moneyPay, wxPay, aliPay,offline,quick, dismiss ->
            it.dismiss()
            if (dismiss) return@setOnResultListener
            when {
                moneyPay == 0 && wxPay == 0 && aliPay == 0 -> "请选择支付方式".showToast()
                wxPay == 1 || aliPay == 1 -> {//三方 或 混合支付
                    context.showLoading()
                    NewPayInfo(orderingInfo.orderInfo.id,
                            wxPay,
                            aliPay,
                            if (moneyPay == 1) orderingInfo.moneyInfo.money else "0",
                            orderingInfo.goodsType,//订单类型
                            "",
                            orderingInfo.deductMoneyPay
                    ).let { info ->
                        pay(info)
                    }
                }
                else -> {//账户支付
                    //余额支付 需要密码框
                    if (UserInfoConfig.havePayPsw()) {
                        showPsw()
                    } else {
                        showSetPayPswTip()
                    }
                }
            }
        }
    }

    override fun showPayDialog(orderingInfo: OrderPayInfo) {
        this.orderingInfo = orderingInfo
        payDialog.setBalance(orderingInfo.moneyInfo.money)//余额
        payDialog.setPayMoney("¥${orderingInfo.moneyPay}")//支付总额
        payDialog.setRadio(AllUtils.moneyCompare(orderingInfo.moneyInfo.money, orderingInfo.moneyPay))//单选 或 多选
        payDialog.show()
    }

}